package com.example.tiku2.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.tiku2.R;
import com.example.tiku2.bean.ProjectBean;


import java.util.List;

public class MainProjectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<ProjectBean.DataBean.DatasBean> mDatas;

    public MainProjectAdapter(Context context, List<ProjectBean.DataBean.DatasBean> datas) {
        mContext = context;
        mDatas = datas;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == 0) {//第一种布局
            View view = View.inflate(mContext, R.layout.rec_item0_main_layout, null);
            return new ViewHolder0(view);
        }else{
            View view = View.inflate(mContext, R.layout.rec_item1_main_layout, null);
            return new ViewHolder1(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        int type = getItemViewType(i);
        if (type == 0) {
            ViewHolder0 holder0 = (ViewHolder0) viewHolder;
            Glide.with(mContext).load(mDatas.get(i).getEnvelopePic()).into(holder0.img_pro);
            holder0.tv_title.setText(mDatas.get(i).getTitle());
        }else {
            ViewHolder1 holder1 = (ViewHolder1) viewHolder;
            holder1.tv_title.setText(mDatas.get(i).getTitle());
            Glide.with(mContext).load(mDatas.get(i).getEnvelopePic()).into(holder1.img_pro);
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLis.onItemClick(i);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        int id = mDatas.get(position).getId();//得到当前数据的id，用于多布局控制
        if (id % 3 == 0){
            return 0;
        }else {
            return 1;
        }

    }

    public class ViewHolder0 extends RecyclerView.ViewHolder {
        ImageView img_pro;
        TextView tv_title;
        public ViewHolder0(@NonNull View itemView) {
            super(itemView);
            img_pro = itemView.findViewById(R.id.img_pro);
            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }
    public class ViewHolder1 extends RecyclerView.ViewHolder {
        TextView tv_title;
        ImageView img_pro;
        public ViewHolder1(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            img_pro = itemView.findViewById(R.id.img_pro);
        }
    }

    private OnItemClickLis mOnItemClickLis;

    public void setOnItemClickLis(OnItemClickLis onItemClickLis) {
        mOnItemClickLis = onItemClickLis;
    }
    public interface OnItemClickLis{
        void onItemClick(int i);

    }
}
